package com.itheima.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.itheima.health.entity.Result;
import cn.itheima.health.resources.MessageConstant;
import cn.itheima.health.service.ReportService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/report")
public class ReportController {

    @Reference
    private ReportService reportService;


    @RequestMapping("/queryMemberByDate.do")
    public Result queryMemberByDate(@RequestParam("beginDate") String beginDate ,@RequestParam("endDate") String endDate ){

        try {


            Map map  = reportService.queryMemberByDate(beginDate,endDate);

           return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);


        } catch (Exception e) {

            e.printStackTrace();
            return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }


    }

    @RequestMapping("/getMemberReport.do")
    public Result getMemberReport(){
        try{
           Map map =  reportService.getMemberReport();
            return  new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @RequestMapping("/getSetmealReport.do")
    public Result getSetmealReport(){
        try{
           List<Map> map =  reportService.getSetmealReport();
            return  new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }

    @RequestMapping("/exportExcel.do")
    public void exportExcel(HttpSession session, HttpServletResponse response) {
    //  easypoi   导出文件数据
        String realPath = session.getServletContext().getRealPath("/template/report_template.xlsx");
        TemplateExportParams params = new TemplateExportParams(realPath);
        Map<String, Object> map = new HashMap<String, Object>(); //  map 模板数据源
        map.put("curdate", "2020-8-5");
        map.put("curdatemember", 2000000.00);
        map.put("curweekmember", 20);
        map.put("totalmemebers",  200);
        map.put("curmonthmemebers", 20);
        map.put("curorderedsettings", 202);
        map.put("curweekorderedsettings", 201);
        map.put("curmonthorderedsettings", 203);
        map.put("curordersettings", 202);
        map.put("curweekordersettings", 209);
        map.put("curmonthordersettings", 202);
        map.put("totalcounts", 2002);

        List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
        for (int i = 1; i <=4; i++) {
            Map<String, String> lm = new HashMap<String, String>();
            lm.put("name","尊享无忧套餐系统："+i);
            lm.put("counts", 100 + "");
            lm.put("zhanbi", "20.25%");
            lm.put("remark", "很好");
            listMap.add(lm);
        }
        map.put("list", listMap);

        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        //  本地磁盘输出文件
//        try {
//            File savefile = new File("D:/excel/");
//            if (!savefile.exists()) {
//                savefile.mkdirs();
//            }
//            FileOutputStream fos = new FileOutputStream("D:/excel/report_template.xls");
//            workbook.write(fos);
//            fos.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
     //    excel浏览器下载
        try {
            //  浏览器下载  一个流   response.getOutputStream()   2个头    附件头 + 文件类型头
            String filename = "report_template.xls";
            String  mimetype = session.getServletContext().getMimeType(filename);
            response.setHeader("Content-Disposition","attachment;filename="+filename);
            response.setContentType(mimetype);
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //用户年龄段占比 饼图
    @RequestMapping("/getMemberPorpertion.do")
    public Result getMemberPorpertion(){

        try {
            List<Map> memberPorpertion =reportService.getMemberPorpertion();
            System.out.println(memberPorpertion);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,memberPorpertion);
        }catch (Exception e){
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }
    //用户性别占比 饼图
    @RequestMapping("/getMemberPorpertionBySex.do")
    public Result getMemberPorpertionBySex(){

        try {
            List<Map> memberPorpertion =reportService.getMemberPorpertionBySex();
            System.out.println(memberPorpertion);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,memberPorpertion);
        }catch (Exception e){
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }
}
